<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="vue.js"></script>
    <link rel="stylesheet" href="./coolDancer.css" />
    <title>Cool-Dancer</title>
  </head>
  <body>
    <div id="app"></div>

    <footer>
      <div class="footer-text">通过方向键操作。按键盘对应的方向键时，相应的图片就会消失。</div>
    </footer>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            downKey: "",
            arrows: [],
            clickedKey: " ",
            showNum: 0,
          };
        },
        provide() {
          return {
            imgs: this.directionImgs,
          };
        },

        methods: {
          newRandom() {
            // 0~99
            return parseInt(Math.random() * 10000) % 100;
          },
          newNum() {
            if (this.newRandom() % 2 === 0) {
              return 6;
            } else {
              return 5;
            }
          },
          newArrows() {
            this.arrows.length = 0;
            this.showNum = 0;
            this.downKey = "";
            for (var i = 0; i < this.newNum(); i++) {
              var number = this.newRandom() % 4;
              // 随机值 0 1 2 3。
              this.arrows.push({
                num: number,
                state: {
                  transition: true,
                  animation0: false,
                  animation1: false,
                  animation2: false,
                  animation3: false,
                  isHide: false,
                },
              });
              this.showNum++;
            }
          },
        },
        created() {
          this.newArrows();
        },
        mounted() {
          document.addEventListener("keydown", (e) => {
            // 左，上，右，下。
            // 37  38 39 40
            // space 32

            this.downKey = e.keyCode;
            if (e.keyCode === 37) this.clickedKey = "左";
            else if (e.keyCode === 38) {
              this.clickedKey = "上";
            } else if (e.keyCode === 39) {
              this.clickedKey = "右";
            } else if (e.keyCode === 40) {
              this.clickedKey = "下";
            }
          });
        },
        computed: {
          getClass() {
            return (index) => {
              if (index < this.arrows.length) {
                return this.arrows[index].state;
              } else {
                return "";
              }
            };
          },
          getArrow() {
            return (index) => {
              if (index < this.arrows.length) {
                return this.arrows[index].num;
              } else {
                return -1;
              }
            };
          },
        },
        watch: {
          showNum(newValue) {
            if (newValue === 0) {
              this.newArrows();
            }
          },
          downKey(newValue, oldValue) {
            if ((newValue !== oldValue && oldValue !== "") || oldValue === "") {
              if (newValue > 36 && newValue < 41) {
                let value = newValue - 37;
                for (const key in this.arrows) {
                  const element = this.arrows[key];
                  if (element.num === value && !element.state.isHide) {
                    element.state["animation" + value] = true;
                    setTimeout(() => {
                      element.state.isHide = true;
                      this.showNum--;
                    }, 800);
                  }
                }
              } else {
              }
            }
          },
        },
        template: `
        <transition >
          <div class="arrowsContainer">
            <arrow :direction="getArrow(0)"  :imgClass="getClass(0)"/>
            <arrow :direction="getArrow(1)"  :imgClass="getClass(1)"/>
            <arrow :direction="getArrow(2)"  :imgClass="getClass(2)"/>
            <arrow :direction="getArrow(3)"  :imgClass="getClass(3)"/>
            <arrow :direction="getArrow(4)"  :imgClass="getClass(4)"/>
            <arrow :direction="getArrow(5)"  :imgClass="getClass(5)"/>
            <arrow :direction="getArrow(6)"  :imgClass="getClass(6)"/>
          </div>
        </transition>
        `,
      });
      app.component("arrow", {
        props: ["direction", "imgClass"],
        data() {
          return {
            directionImgs: [
              {
                src: "./img/left.png",
                direction: "左",
              },
              {
                src: "./img/up.png",
                direction: "上",
              },
              {
                src: "./img/right.png",
                direction: "右",
              },
              {
                src: "./img/down.png",
                direction: "下",
              },
            ],
          };
        },

        computed: {
          getimg() {
            return (direction) => {
              if (direction != -1) return this.directionImgs[direction].src;
              else {
                return "#";
              }
            };
          },
        },
        template: `
        <span class="imgContainer" v-if="direction!=-1"><img :src="getimg(direction)" :class="imgClass"></span>
        `,
      });

      const vm = app.mount("#app");
    </script>
  </body>
</html>
